package com.xj.admin.bussiness.handCertificateBasic.web;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.Validator;
import com.feilong.core.bean.ConvertUtil;
import com.xj.admin.base.index.web.BaseController;
import com.xj.admin.bussiness.handCertificateBasic.service.ITbHandCertificateBasicInfoService;
import com.xj.admin.util.dtgrid.model.Pager;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.exception.EnumSvrResult;
import com.xj.common.base.common.util.JsonUtil;
import com.xj.common.bussiness.handCertificateBasic.entity.TbHandCertificateBasicInfo;
import com.xj.common.bussiness.insure.entity.TbInsure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *    岗位证信息 前端控制器
 * </p>
 *
 * @author zhaohai
 * @since 2017-12-29
 */
@Controller
@RequestMapping("/handcertificatebasicinfo/")
public class TbHandCertificateBasicInfoController extends BaseController {

    @Autowired
    private ITbHandCertificateBasicInfoService handCertificateBasicInfoService;

    @GetMapping("listUI")
    public String listUI() {
        return "handcertificatebasicinfo/list";
    }

    @SuppressWarnings("unchecked")
    @PostMapping("list")
    @ResponseBody
    public Object list(String gridPager) {
        Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
        Map<String, Object> parameters = null;
        if(Validator.isNullOrEmpty(pager.getParameters())){
            parameters = new HashMap<>();
        }else{
            parameters = pager.getParameters();
        }
        Page<TbHandCertificateBasicInfo> list = handCertificateBasicInfoService.selectPage
                (new Page<TbHandCertificateBasicInfo>(pager.getNowPage(), pager.getPageSize()), Condition.create().allEq(parameters).orderBy("id",false));
        makeGridResult(parameters, pager, list);
        return parameters;
    }
    @GetMapping("form")
    public String form(Map<String,Object> map) {
        return "handcertificatebasicinfo/form";
    }

    @RequestMapping(value="all",method= RequestMethod.POST)
    @ResponseBody
    public AbstractBean all(String phone) {
        AbstractBean bean = new AbstractBean();
        List<TbHandCertificateBasicInfo> list = handCertificateBasicInfoService.selectByMap(ConvertUtil.toMap("phone",phone));

        if(list==null){
            bean.setStatus(EnumSvrResult.ERROR.getVal());
            bean.setMessage(EnumSvrResult.ERROR.getName());
        }else{
            bean.setData(list);
        }
        return bean;
    }


    @PostMapping("save")
    @ResponseBody
    public AbstractBean add(TbHandCertificateBasicInfo tbhandCertificateBasicInfo){
        AbstractBean bean = new AbstractBean();
        if (tbhandCertificateBasicInfo.getId() == null) {
            tbhandCertificateBasicInfo.setBirthday(new Date());
            tbhandCertificateBasicInfo.setStartCerDate(new Date());
            tbhandCertificateBasicInfo.setEndCerDate(new Date());
            tbhandCertificateBasicInfo.setStartTrainDate(new Date());
            tbhandCertificateBasicInfo.setEndTrainDate(new Date());
        }
        if(!handCertificateBasicInfoService.insertOrUpdate(tbhandCertificateBasicInfo)){
            bean.setStatus(EnumSvrResult.ERROR.getVal());
            bean.setMessage(EnumSvrResult.ERROR.getName());
        }
        return bean;
    }

    @DeleteMapping("{id}/delete")
    @ResponseBody
    public AbstractBean delete(@PathVariable(required=true) Integer id) {
        if(!handCertificateBasicInfoService.deleteById(id)){
            return fail(EnumSvrResult.ERROR);
        }
        return success();
    }

    @GetMapping("{id}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer id) {
        TbHandCertificateBasicInfo tbhandCertificateBasicInfo = handCertificateBasicInfoService.selectById(id);
        map.put("record", tbhandCertificateBasicInfo);
        return "handcertificatebasicinfo/edit";
    }

    @GetMapping("{id}/info")
    public String info(Map<String,Object> map,@PathVariable(required=true) Integer id) {
        TbHandCertificateBasicInfo tbhandCertificateBasicInfo = handCertificateBasicInfoService.selectById(id);
        map.put("record", tbhandCertificateBasicInfo);
        return "handcertificatebasicinfo/info";
    }

    @DeleteMapping("{ids}/deleteBatch")
    @ResponseBody
    public AbstractBean deleteBatch(@PathVariable(required=true) String ids) {
        if(!handCertificateBasicInfoService.deleteBatchIds(ConvertUtil.toList(ids.split(",")))){
            return fail(EnumSvrResult.ERROR);
        }
        return success();
    }
    //根据id查询所有操作证的信息
    @GetMapping("{id}/details")
    public String details(Map<String,Object> map,@PathVariable(required=true) Integer id){
        TbHandCertificateBasicInfo tbhandCertificateBasicInfo= new TbHandCertificateBasicInfo();
        tbhandCertificateBasicInfo.setId(id);
        List<TbHandCertificateBasicInfo> detailsHandCertificateBasicList= handCertificateBasicInfoService.HandCertificateBasicDetails(tbhandCertificateBasicInfo);
        System.out.println("++++++detailsHandCertificateBasicList的值="+detailsHandCertificateBasicList);
        List<String> detailsList = new ArrayList();
        for (int i=0; i<detailsHandCertificateBasicList.size();i++){
            String remark = detailsHandCertificateBasicList.get(i).getRemark();
            String[] remarkList = remark.split(",");
            for (int j=0;j<remarkList.length;j++){
                System.out.println(remarkList[j]);
                detailsList.add(remarkList[j]);
            }
        }
        map.put("detailsList",detailsList);
        map.put("detailsHandCertificateBasicList", detailsHandCertificateBasicList);
        return "handcertificatebasicinfo/details";
    }
}
